;; =============================================================================
;; type declarations parser

(in-package :oali.core)

;; generic signature
(defgeneric parse-type-decl (context type-id args))

;; anonymous type
(defmethod parse-type-decl (context type-id (args (eql nil)))
  (add-new-type context type-id
		(make-instance 'p-type-proxy)))

;; qualified type: signed
(defmethod parse-type-decl (context (id (eql 'signed)) args)
  (add-new-qualified-type context 'signed args))

;; qualified type: unsigned
(defmethod parse-type-decl (context (id (eql 'unsigned)) args)
  (add-new-qualified-type context 'unsigned args))

;; parse pointer types
(defmethod parse-type-decl (context (id (eql 'ptr)) args)
  (destructuring-bind (existing-type-id new-type-id) args
    (add-new-type context new-type-id
		  (make-instance 'p-type-pointer
				 :pointee-type (existing-type
						context
						existing-type-id)))))

;; type struct ...
(defmethod parse-type-decl (context (id (eql 'struct)) args)
  (destructuring-bind (struct-name-id struct-members) args
    (let (
	  ;; get transformed id-based members
	  (transformed-members
	   (loop for (member-type-id member-name-id) in struct-members
		 collect (make-instance 'p-complex-type-member
					:name (symbol-to-c-id member-name-id)
					:type (existing-type context member-type-id)))))
      ;; create and add new struct
      (add-new-type context struct-name-id
		    (make-instance 'p-type-complex
				   :name (symbol-to-c-id struct-name-id)
				   :members transformed-members)))))
